package cn.dliyy.warehouse.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import cn.dliyy.warehouse.bean.Category;
import cn.dliyy.warehouse.service.CategoryService;

@Controller
@RequestMapping(value = "/category")
public class CategoryController {

	@Resource
	private CategoryService categoryService;
	@Resource
	private Category category;

	public CategoryController() {
	}

	@RequestMapping(value = "/")
	public String load(ModelMap modelMap) {
		List<Object> list = categoryService.getCategoryList();
		modelMap.put("list", list);
		return "/base/category";
		// return "/base/box/categorylist";
	}
	
	
	@RequestMapping(value = "/{pid}")
	public String loadChild(@PathVariable String pid, HttpServletResponse response) {
		List<Object> list = categoryService.getCategoryList(pid);
		if (!list.isEmpty()) {
			StringBuffer outUL = new StringBuffer();
			outUL.append("<ul>");
			Iterator<Object> iter = list.iterator();
			while (iter.hasNext()) {
				Category cate = (Category) iter.next();
				//if ("0".equals(cate.getHasgoods())) {
					outUL.append("<li id='" + cate.getId() + "'><span>" + cate.getName() + "</span>"
							+ "<ul class='ajax'><li>{url:category/" + cate.getId() + "}</li></ul>" + "</li>");// 此种样式为文件夹
				//} else {
					//outUL.append("<li id='" + cate.getId() + "'><span>" + cate.getName() + "</span></li>");// 此种样式为文件
				//}
			}
			outUL.append("</ul>\n");

			PrintWriter out;
			try {
				out = response.getWriter();
				out.print(outUL);
				out.flush();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return null;
	}

	@RequestMapping(value = "/", params = "pid")
	// simple tree 不支持rest格式-使用url:category/{id}时不能正常显示，必须包含&符号
	public String loadChildCate(@RequestParam(value = "pid") String pid, HttpServletResponse response) {
		List<Object> list = categoryService.getCategoryList(pid);
		if (!list.isEmpty()) {
			StringBuffer outUL = new StringBuffer();
			outUL.append("<ul>");
			Iterator<Object> iter = list.iterator();
			while (iter.hasNext()) {
				Category cate = (Category) iter.next();
				//if ("0".equals(cate.getHasgoods())) {
					outUL.append("<li id='" + cate.getId() + "'><span>" + cate.getName() + "</span>"
							+ "<ul class='ajax'><li>{url:category/?a=a&pid=" + cate.getId() + "}</li></ul>" + "</li>");// 此种样式为文件夹
				//} else {
					//outUL.append("<li id='" + cate.getId() + "'><span>" + cate.getName() + "</span></li>");// 此种样式为文件
				//}
			}
			outUL.append("</ul>\n");

			PrintWriter out;
			try {
				out = response.getWriter();
				out.print(outUL);
				out.flush();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return null;
	}

	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String create(ModelMap modelMap) {
		List<Object> list = categoryService.getCategoryList();
		modelMap.put("list", list);
		modelMap.addAttribute("category", category);
		return "/base/category_add";
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public ModelAndView create(@Valid Category category, BindingResult result) {
		ModelAndView mav = new ModelAndView();
		if (result.hasErrors()) {
			mav.setViewName("/base/category_add");
			return mav;
		}
		categoryService.save(category);
		mav.setViewName("redirect:/category/");
		return mav;
	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String load(ModelMap modelMap, String t) {
		List<Object> list = categoryService.getCategoryList();
		modelMap.put("list", list);
		return "/base/box/categorylist";
	}
//设置一个map,key为Category，value为Category的孩子list(按照id大小排序),传入参数Category.id，返回值map.然后写另一个函数，封装成String并返回到ajaxtree.
	//以后在弄吧。
}
